#include <gtest/gtest.h>

#include "../src/dblmdb.hpp"

#include <cstdlib>

class TestLMDB : public ::testing::Test{
protected:
	void SetUp() override{
		db = new DBLMDB(".");
	}

	void TearDown() override{
		delete db;
	}

	DBLMDB *db;
};

TEST_F(TestLMDB, put){
	db->erase();
	uint16_t data[10];
	db->put((char *)data, sizeof(data));
	ASSERT_EQ(db->size(), 1);
	db->put((char *)data, sizeof(data));
	ASSERT_EQ(db->size(), 1);

	DBLMDB *db2 = new DBLMDB(".");
	db2->put(data, sizeof(data));
	ASSERT_EQ(db2->size(), 1);
}

TEST_F(TestLMDB, get){
	uint16_t data[2];
	db->erase();
	for( auto k = 0; k < 1; ++k ){
		for( auto i = 0; i < 100; ++i ){
			for( auto j = 0; j < 100; ++j ){
				data[0] = i; data[1] = j;
				db->put((char *)data, sizeof(data));
			}
		}
		ASSERT_EQ(db->size(), 10000);
	}

	for( int i = 0; i < 10000; ++i )
		ASSERT_TRUE(db->get((char *)data, sizeof(data)));
	ASSERT_EQ(db->size(), 0);
	ASSERT_TRUE(db->empty());
}

TEST_F(TestLMDB, empty){
	ASSERT_EQ(db->size(), 0);
	ASSERT_TRUE(db->empty());
	uint16_t data[10];
	db->put((char *)data, sizeof(data));
	db->erase();
	ASSERT_EQ(db->size(), 0);
	ASSERT_TRUE(db->empty());
}
